package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.constant.BompTreeNodeConstant;
import cc.rengu.igas.bomp.common.dao.BompSequenceMapper;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 商户服务Sequence实现类
 * Created by 王成 on 2018/5/29.
 */
public class BompSequenceMapperImpl implements BompSequenceMapper {
    RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public String getNextMchntNoSeq() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String instId = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.INST_ID);
        String sqlId = "get" + instId + "BompNextMchntNoSeq";
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, sqlId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, null);
        if (0 != iReturnCode) {
            rglog.error("获取商户序号SEQUENCE失败,iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.GENERATE_SEQ_NO_ERROR.getRespCode(), RespCodeEnum.GENERATE_SEQ_NO_ERROR.getRespDesc());
        }
        /* 不足长度前补0 */
        String mchntSeqNum = xmlTreeUtil.getXmlTreeStringValue(BompTreeNodeConstant.MCHNT_SEQ_NUM);
        if (mchntSeqNum.length() < 4) {
            String format = "%0" + (4 - mchntSeqNum.length()) + "d";
            return String.format(format, 0) + mchntSeqNum;
        } else {
            return mchntSeqNum;
        }
    }

    @Override
    public String getNextApplyBatchNo() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getBompNextApplyBatchNo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, null);
        if (0 != iReturnCode) {
            rglog.error("获取批次号SEQUENCE失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        /* 不足长度前补0 */
        String applyBatchSeqNum = xmlTreeUtil.getXmlTreeStringValue(BompTreeNodeConstant.APPLY_BATCH_SEQ_NUM);
        if (applyBatchSeqNum.length() < 8) {
            String format = "%0" + (8 - applyBatchSeqNum.length()) + "d";
            return String.format(format, 0) + applyBatchSeqNum;
        } else {
            return applyBatchSeqNum;
        }
    }


    @Override
    public String getNextQrCodeSeq() throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getBompNextQrCodeSeq");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, null);
        if (0 != iReturnCode) {
            rglog.error("获取二维码序号失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        /* 不足8位长度前补0 */
        String qrCodeSeqNum = xmlTreeUtil.getXmlTreeStringValue(BompTreeNodeConstant.QR_CODE_SEQ_NUM);
        if (qrCodeSeqNum.length() < 8) {
            String format = "%0" + (8 - qrCodeSeqNum.length()) + "d";
            return String.format(format, 0) + qrCodeSeqNum;
        } else {
            return qrCodeSeqNum;
        }
    }
}
